﻿using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using AEM.Utils;

namespace AEM.Data.DAL
{
    public static class User
    {
        public static bool Add(Model.User value)
        {
            string sql = @"INSERT INTO [TableUserAccount]( 
                                                UserName, 
                                                UserPWD, 
                                                UserGroup, 
                                                UserPermission, 
                                                OperateLiftStatus, 
                                                CreateDate, 
                                                LastUpdatePwdDate,
                                                LastLoginDate)
                                            VALUES(  
                                                @UserName, 
                                                @UserPWD, 
                                                @UserGroup, 
                                                @UserPermission, 
                                                @OperateLiftStatus, 
                                                @CreateDate, 
                                                @LastUpdatePwdDate,
                                                @LastLoginDate)";
            int ret = AccessHelper.ExecuteNonQuery(sql, Param(value));
            if (-1 != ret) return true;
            else return false;

        }

        public static Model.User Select(string field, string value)
        {
            string condition = " " + field + "={0}";
            if (field == "ID")
                condition = string.Format(condition, value);
            else
                condition = string.Format(condition, "'" + value + "'");
            DataTable dt = Global.List("TableUserAccount", condition);
            if (null != dt && dt.Rows.Count > 0)
            {
                return Deserialize(dt.Rows[0]);
            }
            return null;
        }

        private static Model.User Deserialize(DataRow r)
        {
            Model.User model = null;
            if (null != r)
            {
                model = new Model.User();
                model.ID = r["ID"].ToString().toInt32();
                model.UserName = r["UserName"].ToString();
                model.UserPWD = r["UserPWD"].ToString();
                model.UserGroup = r["UserGroup"].ToString().toInt32();
                model.UserPermission = r["UserPermission"].ToString().toInt32();
                model.CreateDate = r["CreateDate"].ToString().toDateTime();
                model.LastUpdatePwdDate = r["LastUpdatePwdDate"].ToString().toDateTime();
                model.LastLoginDate = r["LastLoginDate"].ToString().toDateTime();
                model.OperateLiftStatus = r["OperateLiftStatus"].ToString();
            }
            return model;
        }

        private static OleDbParameter[] Param(Model.User args)
        {
            List<OleDbParameter> param = new List<OleDbParameter>();
            param.Add(new OleDbParameter("@UserName", args.UserName));
            param.Add(new OleDbParameter("@UserPWD", args.UserPWD));
            param.Add(new OleDbParameter("@UserGroup", args.UserGroup));
            param.Add(new OleDbParameter("@UserPermission", args.UserPermission));
            param.Add(new OleDbParameter("@OperateLiftStatus", args.OperateLiftStatus));
            param.Add(new OleDbParameter("@CreateDate", args.CreateDate.ToString("yyyy-MM-dd HH:mm:ss")));
            param.Add(new OleDbParameter("@LastUpdatePwdDate", args.LastUpdatePwdDate.ToString("yyyy-MM-dd HH:mm:ss")));
            param.Add(new OleDbParameter("@LastLoginDate", args.LastLoginDate.ToString("yyyy-MM-dd HH:mm:ss")));
            param.Add(new OleDbParameter("@ID", args.ID));
            return param.ToArray();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="field">更新字段</param>
        /// <param name="value">更新字段值</param>
        /// <param name="byfield">条件字段</param>
        /// <param name="byvalue">条件字段值</param>
        public static bool Update(string field, string value, string byfield, string byvalue)
        {
            return Global.Update("TableUserAccount", field, value, byfield, byvalue);
        }

        public static bool Update(Model.User value)
        {
            string sql = @"UPDATE  [TableUserAccount]
                        SET 
                            [UserName] = @UserName, 
                            [UserPWD] = @UserPWD, 
                            [UserGroup] = @UserGroup, 
                            [UserPermission] = @UserPermission, 
                            [OperateLiftStatus] = @OperateLiftStatus, 
                            [CreateDate] = @CreateDate, 
                            [LastUpdatePwdDate] = @LastUpdatePwdDate, 
                            [LastLoginDate] = @LastLoginDate
                        WHERE [ID]=@ID";

            int ret = AccessHelper.ExecuteNonQuery(sql, Param(value));
            if (ret > 0) return true;
            return false;

        }

        public static List<Model.User> List(string condition)
        {
            DataTable dt = Global.List("TableUserAccount", condition);
            if (null != dt && dt.Rows.Count > 0)
            {
                return Deserialize(dt);
            }
            return null;
        }

        private static List<Model.User> Deserialize(DataTable dt)
        {
            List<Model.User> list = null;
            if (null != dt && dt.Rows.Count > 0)
            {
                list = new List<Model.User>();
                foreach (DataRow r in dt.Rows)
                {
                    list.Add(Deserialize(r));
                }
            }
            return list;
        }

        public static bool Delete(int id)
        {
            return Global.Delete("TableUserAccount", "ID", id.ToString());
        }
    }
}
